package com.umanji.util;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Validation;
import javax.validation.Validator;

import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Handles requests for the application welcome page.
 */
@Controller
@RequestMapping("/util")
public class UtilController {

	private Logger logger = org.slf4j.LoggerFactory.getLogger(UtilController.class);

	private Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

	@RequestMapping(value = "/crossDomainAjaxPage", method = RequestMethod.GET)
	public @ResponseBody String getCrossDomainAjaxPage(@RequestParam("url") String url, final HttpServletResponse response) {
		logger.info("get cross domain page !");

		return Util.getCrossDomainPageForAjax(url);
	}

	@RequestMapping(value = "/getWmsQueryPage", method = RequestMethod.GET)
	public @ResponseBody String getWmsQuery(final HttpServletRequest request, final HttpServletResponse response) {
		logger.info("get cross domain page !");

		StringBuffer urlParamater = new StringBuffer("?");
		Map<String,String[]> urlMap = request.getParameterMap();
	    Iterator iterator = urlMap.keySet().iterator();
	    while (iterator.hasNext()) {
	      String key = (String) iterator.next();
	      String[] val = urlMap.get(key);
	      urlParamater.append(key);
	      urlParamater.append("=");
	      urlParamater.append(val[0]);
	      if(iterator.hasNext()) {
	    	  urlParamater.append("&");
	      } else {
	    	  
	      }
	    }
	    logger.info("full url : " + urlParamater.toString());
	    String returnPage = Util.getCrossDomainPageForAjax("http://www.umanji.com/geowebcache/service/wms" + urlParamater.toString());
	    // logger.info("return page : " + returnPage);
		return returnPage;
	}
}
